<?php
namespace Admin\Model;
use Think\Model;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 9:23
 */
class MenuModel extends Model
{

    //获取左边侧边栏
    public function getMenuAll(){
        $arr = M("menu")->where(array("status"=>1))->select();
        $str = $this->doData($arr,$pid=0);
        return $str;
    }
    //处理数据拼装字串
    public function doData($arr=array(),$pid=0){
        static $str_menu = '<ul class="nav nav-list">';

        $data = $this->getData($arr,$pid);
        foreach($data as $key=>$val){
            $pid_arr = $this->getData($arr,$val['id']);
            if(!empty($pid_arr)){
                $str_menu .= '<li><a class="dropdown-toggle" href="'.U($val['url']).'"><i class="'. $val['icon'] .'"></i><span class="menu-text">'.$val['name'].'</span><b class="arrow icon-angle-down"></b></a>';
                $str_menu .= '<ul class="submenu">';
                $this->doData($arr, $val['id']);
                $str_menu .= '</ul></li>';
            }else{
                $str_menu .= '<li><a href="'.U($val['url']).'"><i class="'. $val['icon'] .'"></i><span class="menu-text">'.$val['name'].'</span></a></li>';
            }
        }
        return $str_menu;

    }
    //处理数据，模拟sql查询
    public function getData($data,$pid){
        $res = array();
        foreach($data as $v){
            if($v['pid'] == $pid){
                $res[] = $v;
            }
        }
        return $res;
    }

    public function getTree($model,$pid){
        $arr = array();
        $list = $model->where(array('status'=>1,'pid'=>$pid))->order('sort')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $arr[$k] = $v;
                if(is_array($v)){
                    $sub =getTree($model, $v['id']);
                    $arr[$k]['child'] = $sub;
                }
            }
        }
        return $arr;
    }


    public function getMenuTree($pid=0, $level){
        static $str='<ul class="nav nav-list">';
        $list = M('menu')->where(array('status'=>1,'pid'=>$pid))->order('sort asc,id asc')->select();  //dump($list);die;
        if(!empty($list)){
            foreach($list as $k=>$v){
                if($v['is_active'] == 1){
                    $li = '<li class="active">';
                }else{
                    $li = '<li>';
                }
                $i = M('menu')->where(array('status'=>1,'pid'=>$v['id']))->select();
                if(!empty($i)){
                    $str .= $li .'<a class="dropdown-toggle" href="'.U($v['url']).'"><i class="'. $v['icon'] .'"></i><span class="menu-text">'.$v['name'].'</span><b class="arrow icon-angle-down"></b></a>';
                    $str .= '<ul class="submenu">';
                    $sub = getMenuTree($v['id'], $level+1);
                    $str .= '</ul></li>';

                }else{
                    $str .= $li . '<a href="'.U($v['url']).'"><i class="'. $v['icon'] .'"></i><span class="menu-text">'.$v['name'].'</span></a></li>';
                }

            }
        }

        return $str;
    }

    public function getMenuTree1($pid=0, $level=0){
        $arr = array();
        static $str='';
        $list = M('menu')->where(array('status'=>1,'pid'=>$pid))->order('sort')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $arr[$k] = $v;
                $str .= str_repeat('--', $level) .$v['name'].'<br />';
                if(is_array($v)){
                    $sub =getMenuTree($v['id'], $level+1);
                    $arr[$k]['child'] = $sub;

                }
            }
        }
        return $str;
    }



    public function doDataArr($arr,$pid){
        $res = array();
        foreach($arr as $key=>$val){
            $count = $this->countNum($arr,$val['id']);
            if($count > 0){
                $res[$val['pid']][$key]=$val;

            }
        }
        dump($res);
    }
    public function countNum($data =array(),$num=0){
        $i =0;
        if(empty($data)){
            return $i;
        }else{
            foreach($data as $v){
                if($num==$v['pid']){
                    $i++;
                }
            }
        }
        return $i;
    }


}